package com.qzsoft.zhjd.base.modules.parking.dto;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.zhjd.base.modules.parking.entity.BaseParkingEntity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @ClassName BaseParkingDto
 * @Description 采集端发送过来的数据
 * @author linhuawei
 * @since 2021-12-16
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class BaseParkingReceiveDto {

    /**
     * 主键
     */
    private String originalId;

    /**
     * 车牌号
     */
    private String carNumber;

    /**
     * 车牌颜色
     */
    private String carPlateColor;

    /**
     * 车辆类型
     */
    private String carType;

    /**
     * 车辆类型
     */
    private String carKind;

    /**
     * 进场时间
     */
    private String enterTime;

    /**
     * 进场车道
     */
    private String enterDoorname;

    /**
     * 出场时间
     */
    private String leaveTime;

    /**
     * 出场车道
     */
    private String leaveDoorname;

    /**
     * 当前状态
     */
    private String state;

    public BaseParkingEntity coverToEntity() {
        BaseParkingEntity entity = new BaseParkingEntity();
        entity.setOriginalId(originalId);
        entity.setCarKind(carKind);
        entity.setCarNumber(carNumber);
        entity.setCarPlateColor(carPlateColor);
        entity.setCarType(carType);
        entity.setEnterDoorname(enterDoorname);
        entity.setLeaveDoorname(leaveDoorname);
        entity.setState(state);

        // 入场时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date enterDate = null;
        if (ObjectUtils.isNotEmpty(enterTime)) {
            try {
                enterDate = sdf.parse(enterTime);
            } catch (ParseException e) {
                enterDate = null;
            }
        }
        entity.setEnterTime(enterDate);

        // 离场时间
        Date leaveDate = null;
        if (ObjectUtils.isNotEmpty(leaveTime)) {
            try {
                leaveDate = sdf.parse(leaveTime);
            } catch (ParseException e) {
                leaveDate = null;
            }
        }
        entity.setLeaveTime(leaveDate);
        
        // 最后停留时间
        if(null != leaveDate) {
            entity.setLastTime(leaveDate);
        } else {
            entity.setLastTime(enterDate);
        }
        
        // 停留时长
        if(null != leaveDate && null != enterDate) {
            long duration = (leaveDate.getTime() - enterDate.getTime()) / 1000L;
            entity.setDuration(duration);
        }

        return entity;
    }

}
